/*
<Copyright (C) 2011 Dru Moore>

This file is part of demorecorder.

vlc-remote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

demorecorder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with demorecorder.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "tracklistmodel.h"
#include <QHash>
#include <QList>
#include <QDebug>

TrackListModel::TrackListModel(QObject *parent) :
    QAbstractListModel(parent)
{
    QHash<int, QByteArray> roles;
    roles[UidRole] = "uid";
    roles[TitleRole] = "title";
    roles[IconRole] = "icon";
    roles[ColorRole] = "color";
    roles[IsMutedRole] = "isMuted";
    roles[IsActiveRole] = "isActive";
    setRoleNames(roles);

    this->trackListItemList = new QList<TrackListItem>();
}

int TrackListModel::rowCount(const QModelIndex &parent) const {
    return this->trackListItemList->count();
}
int TrackListModel::rowCount() const {
    return this->trackListItemList->count();
}

QVariant TrackListModel::data(const QModelIndex &index, int role) const {
    if (-1 < index.row() && this->trackListItemList->count() > index.row()) {
        switch(role) {
            case UidRole:
                return this->trackListItemList->at(index.row()).uid;
            case TitleRole:
                return this->trackListItemList->at(index.row()).title;
            case IconRole:
                return this->trackListItemList->at(index.row()).icon;
            case ColorRole:
                return this->trackListItemList->at(index.row()).color;
            case IsMutedRole:
                return this->trackListItemList->at(index.row()).isMuted;
            case IsActiveRole:
                return this->trackListItemList->at(index.row()).isActive;
            default:
                return NULL;
        }
    }
    return NULL;

}

bool TrackListModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    if (index.isValid()) {
        TrackListItem item = TrackListItem(this->trackListItemList->at(index.row()));
        switch(role) {
            case UidRole:
                item.uid = value.toString();
                break;
            case TitleRole:
                item.title = value.toString();
                break;
            case IconRole:
                item.icon = value.toString();
                break;
            case ColorRole:
                item.color = value.toString();
                break;
            case IsMutedRole:
                item.isMuted = value.toBool();
                break;
            case IsActiveRole:
                item.isActive = value.toBool();
                break;
            default:
                return false;
        }
        this->trackListItemList->replace(index.row(), item);
        emit dataChanged(index, index);
        return true;
    }

    return false;
}

void TrackListModel::addTrackListItem(TrackListItem item) {
    this->trackListItemList->append(item);
}
